package com.github.shiqiyue.app.api.gapi.common;


import com.github.shiqiyue.app.common.exception.BusinessException;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.config.security.gapi.GapiLoginUser;
import com.github.shiqiyue.app.config.security.gapi.GapiSecurityContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.validation.ConstraintViolationException;
import java.sql.SQLException;

/***
 * api 控制器基类
 * @author wenyao.wu
 * @date 2019/7/8
 */
@Slf4j
public abstract class GApiBaseController implements ApplicationContextAware {

    /***
     * 对象为空，返回的消息
     */
    public static final String MESSAGE_OBJECT_NULL = "对象为null";

    /***
     * applicationContext
     */
    private ApplicationContext applicationContext;

    /***
     * 业务异常
     * @param bussionException
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result onBussinessException(BusinessException bussionException) {
        log.error("业务异常:", bussionException);
        return Result.fail(bussionException.getMessage());
    }

    /***
     * 参数异常
     * @param exception
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result onConstraintViolationException(ConstraintViolationException exception) {
        log.error("参数异常:", exception);
        return Result.fail(exception.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public Result onBindException(BindException bindException) {
        log.error("异常:", bindException);
        return Result.fail(bindException.getAllErrors().get(0).getDefaultMessage());
    }


    /***
     * SQL异常
     * @param sqlException
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public Result onSqlException(SQLException sqlException) {
        log.error("sql异常:", sqlException);
        return Result.fail("系统异常，请联系管理员");
    }

    /***
     * 异常
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result onException(Exception exception) {
        log.error("其他异常:", exception);
        return Result.fail("系统异常，请稍后再试");
    }

    /***
     * 获取applicationContext
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


    /***
     * 获取登录的用户信息
     * @return
     */
    public GapiLoginUser getCurrentLoginUser() {
        try {
            return GapiSecurityContext.getCurrentLoginUser();
        } catch (Exception e) {
            return null;
        }
    }


}
